package com.pine.web.pineframework.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.DesensitizedUtil;
import com.pine.common.beans.ResultVo;
import com.pine.common.beans.pineframework.request.SysDecryptAndDesensitizeRequest;
import com.pine.common.beans.request.DataRequest;
import com.pine.common.database.config.MybatisPlusCustomProperties;
import com.pine.common.util.sensitive.SensitiveUtil;
import com.pine.web.core.WithoutLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 加解密前端控制器
 * </p>
 *
 * @author pine manage
 * @since 2024-10-09
 */
@Tag(name = "加解密前端控制器")
@RestController
@RequestMapping("/sys/sensitive")
public class SysSensitiveDataController {

    @Resource
    private MybatisPlusCustomProperties mybatisPlusCustomProperties;


    /**
     * 加密
     *
     * @param request 请求
     * @return 响应
     */
    @Operation(summary = "加密")
    @PostMapping("/encrypt")
    @WithoutLogin
    @SaIgnore
    public ResultVo<String> encrypt(@RequestBody @Valid DataRequest<String> request) {
        String sensitiveTextKey = mybatisPlusCustomProperties.getSensitiveTextKey();
        String encryptText = SensitiveUtil.aesEncrypt(request.getData(), SensitiveUtil.getFixedKey(sensitiveTextKey));
        return ResultVo.success(encryptText);
    }

    /**
     * 解密
     *
     * @param request 请求
     * @return 响应
     */
    @Operation(summary = "解密")
    @PostMapping("/decrypt")
    @WithoutLogin
    @SaIgnore
    public ResultVo<String> decrypt(@RequestBody @Valid DataRequest<String> request) {
        String sensitiveTextKey = mybatisPlusCustomProperties.getSensitiveTextKey();
        String decryptText = SensitiveUtil.aesDecrypt(request.getData(), SensitiveUtil.getFixedKey(sensitiveTextKey));
        return ResultVo.success(decryptText);
    }


    /**
     * 解密并脱敏
     *
     * @param request 请求
     * @return 响应
     */
    @Operation(summary = "解密并脱敏")
    @PostMapping("/decryptAndDesensitize")
    @WithoutLogin
    @SaIgnore
    public ResultVo<String> decryptAndDesensitize(@RequestBody @Valid SysDecryptAndDesensitizeRequest request) {
        String sensitiveTextKey = mybatisPlusCustomProperties.getSensitiveTextKey();
        String decryptText = SensitiveUtil.aesDecrypt(request.getData(), SensitiveUtil.getFixedKey(sensitiveTextKey));
        DesensitizedUtil.DesensitizedType desensitizedType = request.getDesensitizedType();
        return ResultVo.success(DesensitizedUtil.desensitized(decryptText, desensitizedType));
    }


    /**
     * 获取敏感数据前缀
     *
     * @return 响应
     */
    @Operation(summary = "获取敏感数据前缀")
    @PostMapping("/getSensitiveTextPrefix")
    @WithoutLogin
    @SaIgnore
    public ResultVo<String> getSensitiveTextPrefix() {
        String sensitiveTextPrefix = mybatisPlusCustomProperties.getSensitiveTextPrefix();
        return ResultVo.success(sensitiveTextPrefix);
    }
}
